package cn.yhjz.platform.system.controller;

import cn.yhjz.common.AjaxResponse;
import cn.yhjz.platform.system.entity.SysUserRole;
import cn.yhjz.platform.system.service.ISysUserRoleService;
import cn.yhjz.platform.system.vo.SysUserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author maguoping
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/system/sysUserRole")
@PreAuthorize("hasAuthority('system:user-role')")
public class SysUserRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @RequestMapping("queryRoleCodeByUserCode")
    public AjaxResponse queryRoleCodeByUserCode(String userCode) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("user_code", userCode);
        List<SysUserRole> sysUserRoleList = sysUserRoleService.listByMap(queryMap);
        List<String> roleCodeList = sysUserRoleList.stream().map((ur) -> ur.getRoleCode())
                .collect(Collectors.toList());
        return AjaxResponse.success(roleCodeList);
    }

    /**
     * 新增用户
     *
     * @param sysUserRoleVo
     * @return
     */
    @PostMapping("saveOrUpdate")
    public AjaxResponse saveOrUpdate(@RequestBody SysUserRoleVo sysUserRoleVo) {
        int res = sysUserRoleService.saveUserRole(sysUserRoleVo.getUserCode(), sysUserRoleVo.getRoleCodeList());
        return AjaxResponse.success("设置用户角色成功");

    }
}
